package com.sensePlusVR.manager;

import java.util.HashMap;
import java.util.Map;

public class BaseManager {

	public static final int EVENT_DATA_GETTED = 0;
	public static final int EVENT_DATA_UPDATED = 1;
	public static final int EVENT_DATA_CHANGED = 2;
	protected static Object lockListener;
	protected static Object lockDataCenterEvent;
	//protected BeanEventListener listener;
	//protected Object            params;
	protected int localCurrPage = 0;
	//protected Map<String, BeanEventListener> lmap = new HashMap<String, BeanEventListener>();
	//protected Map<String, Object> pmap = new HashMap<String, Object>();
	protected Map<String, EventManager> events = new HashMap<String, EventManager>();
	protected Map<String, EventManager> dataCenterevents = new HashMap<String, EventManager>();
	
	public BaseManager(){
		lockListener = new Object();
		lockDataCenterEvent = new Object();
	}
	
	
	public void addDataCenterEventListener(EventListener l, Object p, String key){
		synchronized(lockDataCenterEvent){
			if (l != null) {
				EventManager e = new EventManager(l, p);
				dataCenterevents.put(key, e);
			}
		}
	}
	
	public void delDataCenterEventListener(String key){
		synchronized(lockDataCenterEvent){
			if (events.containsKey(key)) {
				dataCenterevents.remove(key);
			}
		}
	}
	
	private void callDataCenterEventListener(int event, Object data){
		synchronized(lockDataCenterEvent){
			for (String key:dataCenterevents.keySet()){
				EventManager m = dataCenterevents.get(key);
				if (m != null){
					m.l.Notify(event, data, m.p);
				}
			}
		}
	}
	
	public void addEventListener(EventListener l, Object p, String key){
		synchronized(lockListener){
			if (l != null) {
				EventManager e = new EventManager(l, p);
				events.put(key, e);
			}
		}
	}
	
	public void delEventListener(String key){
		synchronized(lockListener){
			if (events.containsKey(key)) {
				events.remove(key);
			}
		}
	}
	
	private void callUserEventListener(int event, Object data){
		synchronized(lockListener){
			for (String key:events.keySet()){
				EventManager m = events.get(key);
				if (m != null){
					m.l.Notify(event, data, m.p);
				}
			}
		}
	}
	
	public void callEventListener(int event, Object data){
		callDataCenterEventListener(event, data);
		callUserEventListener(event, data);
	}
	
	public class EventManager{
		public EventListener l;
		public Object            p;
		
		public EventManager(EventListener l, Object p){
			this.l = l;
			this.p = p;
		}
	}
}
